/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package it.polimi.deib.se2.meteocal.business.boundary;

import it.polimi.deib.se2.meteocal.business.boundary.NotificationManager;
import it.polimi.deib.se2.meteocal.business.entity.Notification;
import javax.ejb.EJB;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import org.jboss.arquillian.container.test.api.Deployment;
import org.jboss.arquillian.junit.Arquillian;
import org.jboss.shrinkwrap.api.ShrinkWrap;
import org.jboss.shrinkwrap.api.asset.EmptyAsset;
import org.jboss.shrinkwrap.api.spec.WebArchive;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;
import org.junit.Test;
import org.junit.runner.RunWith;

@RunWith(Arquillian.class)
public class NotificationManagerIT {
    
    @EJB
    NotificationManager nm;
    
    @PersistenceContext
    EntityManager entity;
    
    @Deployment
    public static WebArchive createArchiveAndDeploy() {
        return ShrinkWrap.create(WebArchive.class)
                .addClass(NotificationManager.class)
                .addPackage(Notification.class.getPackage())
                .addAsResource("test-persistence.xml", "META-INF/persistence.xml")
                .addAsWebInfResource(EmptyAsset.INSTANCE, "beans.xml");
    }
    @Test
    public void EventManagerShouldBeInjected() {
        assertNotNull(nm);
    }
    
    @Test
    public void EntityManagerShouldBeInjected() {
        assertNotNull(entity);
    }
    
    @Test
    public void newNotificationShouldHaveId() {
        Notification notif = new Notification();
        notif.setId(null);
        try {
            nm.save(notif);
        } catch (Exception e) {
            assertTrue(true);
        }
    }
    @Test
    public void noSameIdForTwoNotifications() {
        Notification n1 = new Notification();
        Notification n2 = new Notification();
        n1.setId(Long.MIN_VALUE);
        n2.setId(Long.MIN_VALUE);
        try {
            nm.save(n1);
            nm.save(n2);
        } catch(Exception e) {
            assertTrue(true);
        }
    }
    
    @Test
    public void newNotificationsShouldBelongToEvent() {
        Notification notif = new Notification();
        
        try{
            notif.getReferingEvent();
        }catch(NullPointerException e ){
            assertTrue(true);
        }
    }
    @Test
    public void notificationsShouldRefertToInvitation(){
        Notification notif = new Notification();
        notif.setInvitation(null);
        try{
            nm.save(notif);
        }catch(Exception e){
           assertTrue(true);

        }
        
    }
    //quest0 fallisce
    /*@Test
    public void findNotificationReturnsOneNotificationIfItExists(){
        Notification notification = new Notification();
        notification.setId(Long.MIN_VALUE); 
        nm.save(notification);   
        assertSame(Long.MIN_VALUE,nm.find(Long.MIN_VALUE).getId());
    }*/
    @Test
    public void FindNotificationCanNotFindValueIfItDoesNotExist(){
        Notification n= new Notification();
        n.setId(Long.MIN_VALUE);
        try{
            nm.find(n.getId());
        }catch(NullPointerException e){
            assertTrue(true);
        }
    }
    
}
